//
//  ZCXNewPetVideoDetialViewController.m
//  LoveOfPet
//
//  Created by dllo on 15/11/14.
//  Copyright © 2015年 孔维. All rights reserved.
//

#import "ZCXNewPetVideoDetialViewController.h"
#import "ZCXNewPetDetialTableViewCell.h"
#import "ZFHYNetWork.h"
#import "ZCXMVModel.h"
#import <MJRefresh.h>
#import "ZCXUserDetialViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "FileManage.h"
#import "ZCXPlayerViewController.h"
#import <SVProgressHUD.h>
#define HEIGHT  [[UIScreen mainScreen]bounds].size.height / 667
#define WIDTH   [[UIScreen mainScreen]bounds].size.width / 375
@interface ZCXNewPetVideoDetialViewController ()<UITableViewDataSource, UITableViewDelegate, ZCXNewPerDetialTableViewCellDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *mvModelArray;
@property (nonatomic, retain) MJRefreshBackGifFooter *footer;
@property (nonatomic, assign) NSInteger pageNumber;
@property (nonatomic, retain) NSString *str;
@property (nonatomic, copy) NSString *path;
@property (nonatomic, assign) BOOL fun;
@end
@implementation ZCXNewPetVideoDetialViewController
- (void)dealloc{
    [_titleName release];
    [_tableView release];
    [_mvModelArray release];
    [_str release];
    [_footer release];
    [_path release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _pageNumber = 1;
    self.mvModelArray = [NSMutableArray array];
    //设置头标题
    self.title = self.titleName;
    self.view.backgroundColor = [UIColor whiteColor];
     self.navigationController.navigationBar.tintColor = [UIColor blackColor];
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"Zfanhui1"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonAction:)];
    
    self.path = [FileManage createFile:@"newPet.plist"];
    NSDictionary *dic = [FileManage readFile:self.path];
    NSArray *mvArray = [dic objectForKey:@"mv"];
    for (NSDictionary *mvDic in mvArray) {
        ZCXMVModel *mvModel = [ZCXMVModel ZCXMVModelWithDictionary:mvDic];
        [self.mvModelArray addObject:mvModel];
    }
    //请求数据
    self.str = [self.titleName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [self loadDataWithString:[NSString stringWithFormat:@"http://newapi.meipai.com/search/user_mv.json?count=20&page=1&q=%@", self.str]];
    self.automaticallyAdjustsScrollViewInsets = NO;
    //tableView创建
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width , self.view.frame.size.height - 64 * HEIGHT) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
    //注册Cell
    [_tableView registerClass:[ZCXNewPetDetialTableViewCell class] forCellReuseIdentifier:@"newpetvideodetialtableviewcell"];
    [_tableView release];
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _fun = NO;
        [self loadDataWithString:[NSString stringWithFormat:@"http://newapi.meipai.com/search/user_mv.json?count=20&page=1&q=%@", self.str]];
    }];
    [self Footer];
}
#pragma mark 请求数据
- (void)loadDataWithString:(NSString *)string{
    
    [ZFHYNetWork handleGETWithUrlString:string parameters:nil showHuD:YES onView:nil successfulBlock:^(id responseObject) {
        NSDictionary *dic = responseObject;
        if (_fun == NO) {
            [self.mvModelArray removeAllObjects];
        }
        [FileManage writeFile:dic andFilePath:self.path];
        NSArray *mvArray = [dic objectForKey:@"mv"];
        for (NSDictionary *mvDic in mvArray) {
            ZCXMVModel *mvModel = [ZCXMVModel ZCXMVModelWithDictionary:mvDic];
            [self.mvModelArray addObject:mvModel];
        }
        [self.tableView.header endRefreshing];
        [self.footer endRefreshing];
        [_tableView reloadData];
    } failureBlock:^(NSError *error) {
        NSLog(@"失败");
    }];
}
#pragma mark 上拉加载
-(void)Footer{
    self.footer = [MJRefreshBackGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    self.tableView.footer = self.footer;
}
- (void)footerRefresh{
     _fun = YES;
    _pageNumber += 1;
    [self loadDataWithString:[NSString stringWithFormat:@"http://newapi.meipai.com/search/user_mv.json?count=20&page=%ld&q=%@", _pageNumber, self.str]];
}
//- (void)buttonAction:(UIButton *)button{
//    [self.navigationController popToRootViewControllerAnimated:YES];
//}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZCXNewPetDetialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newpetvideodetialtableviewcell" forIndexPath:indexPath];
//        cell.myBlock = ^(NSString *video){
//            if (![video isEqualToString:self.mvModelArray[indexPath.section]]) {
//                cell.contactView.hidden = NO;
//                cell.pauseImageV.hidden = NO;
//                [cell.play pause];
//                cell.fun = NO;
//            }
//        };
    
    cell.delegate = self;
    //设置选中状态
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.mvModel = self.mvModelArray[indexPath.section];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.view.frame.size.height / 2 + 84 * HEIGHT;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.mvModelArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}
- (void)valueWithAVplayer:(ZCXMVModel *)player{
    ZCXPlayerViewController *playerViewColltroller = [[ZCXPlayerViewController alloc]init];
    playerViewColltroller.mvModel = player;
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:playerViewColltroller animated:YES];
    [playerViewColltroller release];
   
}
#pragma mark 代理方法
- (void)pushIntoUser:(NSString *)str{
    ZCXUserDetialViewController *userDetial = [[ZCXUserDetialViewController alloc]init];
    self.hidesBottomBarWhenPushed = YES;
    userDetial.strID = str;
    userDetial.titleName = self.titleName;
    
    [self.navigationController pushViewController:userDetial animated:YES];
    [userDetial release];
}
- (void)viewWillDisappear:(BOOL)animated{
    [SVProgressHUD dismiss];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
